Imports System.Net.Mail
Partial Class _Default
    Inherits System.Web.UI.Page

    Dim UnfinishedDate As New Date


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Sql As String
        Dim CountSql As String
        Dim Params(0) As GenParameter

        WelcomeLabel.Text = "Welcome " & Request.Cookies("Profile").Item("ExecutiveName").ToString
        lblDesignation.Text = Request.Cookies("Profile").Item("Designation").ToString


        'Don't show the controls if the password has not been changed
        If Request.Cookies("Profile").Item("TestPass") = True Then
            pnlMarketing.Visible = False
            pnlMgmt.Visible = False
            ChangePassLabel.Visible = True
        Else
            ChangePassLabel.Visible = False
            pnlMarketing.Visible = True

        End If

        If Request.Cookies("Profile").Item("AccessID") = 1 Then
            pnlMgmt.Visible = False
        End If


        Sql = "SELECT DATE_FORMAT(M.DateofNextAction,'%M %d, %Y') AS 'Date', Count(*) AS 'Unfinished Units'" & _
            " FROM ActionMaster INNER JOIN (ClientMaster INNER JOIN (ExecutiveMaster INNER JOIN (ProjectMaster INNER JOIN (StatusMaster INNER JOIN WorkflowMaster AS M ON StatusMaster.StatusID = M.StatusID) ON ProjectMaster.ProjectID = M.ProjectID) ON ExecutiveMaster.ExecutiveID = M.ExecutiveID) ON ClientMaster.ClientID = M.ClientID) ON ActionMaster.ActionID = M.ActionID" & _
            " WHERE M.ClientID NOT IN (Select ClientID from WorkflowMaster where InteractionID > M.InteractionID) AND ExecutiveMaster.ExecutiveUserName = ?ExecutiveName AND M.DateofNextAction < DATE_ADD(NOW(), INTERVAL 630 MINUTE)" & _
            " AND M.NextAction NOT LIKE '%Nothing%' AND ProjectMaster.Completed = 0" & _
            " GROUP BY Date ORDER BY M.DateofNextAction desc"

        CountSql = "Select Count(*) FROM (SELECT DATE_FORMAT(M.DateofNextAction,'%M %d, %Y') AS 'Date', Count(*) AS 'Unfinished Units'" & _
            " FROM ActionMaster INNER JOIN (ClientMaster INNER JOIN (ExecutiveMaster INNER JOIN (ProjectMaster INNER JOIN (StatusMaster INNER JOIN WorkflowMaster AS M ON StatusMaster.StatusID = M.StatusID) ON ProjectMaster.ProjectID = M.ProjectID) ON ExecutiveMaster.ExecutiveID = M.ExecutiveID) ON ClientMaster.ClientID = M.ClientID) ON ActionMaster.ActionID = M.ActionID" & _
            " WHERE M.ClientID NOT IN (Select ClientID from WorkflowMaster where InteractionID > M.InteractionID) AND ExecutiveMaster.ExecutiveUserName = ?ExecutiveName AND M.DateofNextAction < DATE_ADD(NOW(), INTERVAL 630 MINUTE)" & _
            " AND M.NextAction NOT LIKE '%Nothing%' AND ProjectMaster.Completed = 0" & _
            " GROUP BY Date ORDER BY M.DateofNextAction) AS test"

        Params(0) = New GenParameter("?ExecutiveName", User.Identity.Name, Data.DbType.String)

        cstmUnfinished.Sql = Sql
        cstmUnfinished.CountSql = CountSql
        cstmUnfinished.Params = Params
        cstmUnfinished.ShowButton = False
        cstmUnfinished.Size = 10
        cstmUnfinished.CommandButton = True

    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
        Page.Title = "Welcome Page"
    End Sub

    Public Sub ButtonPress(ByVal SelectedRow As GridViewRow) Handles cstmUnfinished.ButtonPress
        Dim DateCell As TableCell = SelectedRow.Cells(1)

        Try

            UnfinishedDate = DateCell.Text

        Catch ex As System.InvalidCastException

            Dim Mail As New MailMessage
            Dim Message As String

            Mail.From = New MailAddress(User.Identity.Name)
            Mail.To.Add("bhagwad@zakgroup.com")
            Message = ex.InnerException.ToString & " " & ex.Message & " " & ex.StackTrace.ToString
            Mail.Subject = "Error in Casting Date"

            Mail.Body = Message & "The Uncastable Date is :" & DateCell.Text

            Dim smtp As New SmtpClient("66.180.175.203")
            smtp.Send(Mail)

            Response.Redirect("~/default.aspx")


        End Try



        Server.Transfer("~/Presentation/Unfinished.aspx")
    End Sub

    Public ReadOnly Property getDate() As Date

        Get
            Return UnfinishedDate
        End Get

    End Property



End Class
